/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.dataaccess.hibernate;

import cassys.dataaccess.DAOException;
import cassys.dataaccess.StudentDAO;
import cassys.datatransferobjects.StudentDTO;
import cassys.datatransferobjects.UserDTO;
import cassys.exceptions.HibernateAccessLayerException;
import java.util.ArrayList;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Priyanka
 */
public class StudentDAOImpl extends AbstractDao implements StudentDAO {

    @Override
    public void insertStudent(StudentDTO student) {
       try {
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Updating User" + student.getUsername()); 
            super.saveOrUpdate(student);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
    }

    @Override
    public void updateStudent(StudentDTO student) {
       try {
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Updating User" + student.getUsername()); 
            super.saveOrUpdate(student);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
    }

    @Override
    public void deleteStudent(StudentDTO student) {
         try {
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Deleting User" + student.getUsername());
            super.delete(student);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
    }

    @Override
    public StudentDTO findStudent(String studentid) {
       StudentDTO p = null;
       try {
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Finding User" + studentid);
            if (studentid == null)
                return p;
            p = (StudentDTO) super.find(StudentDTO.class, studentid);
            
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
       return p;
    }

    @Override
    public ArrayList<StudentDTO> findAllStudent() {
        try {
            return (ArrayList<StudentDTO>) super.findAll(StudentDTO.class);
            } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
        }
    }

    
    
   @Override
    public void insertOrUpdateUser(UserDTO usr) {
           try {
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Updating User" + usr.getUsername()); 
            super.saveOrUpdate(usr);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
    }

    @Override
    public void deleteUser(UserDTO usr) {
        try {
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Deleting User" + usr.getUsername());
            super.delete(usr);
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
    }

    @Override
    public UserDTO findUser(String username) {
          try {
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Finding User" + username);
            UserDTO p = (UserDTO) super.find(UserDTO.class, username);
            return p;
        } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
            
        }
        
    }

    @Override
    public ArrayList<UserDTO> findAllUser() {
        try {
            return (ArrayList<UserDTO>) super.findAll(UserDTO.class);
            } catch (Throwable t) {
            throw new HibernateAccessLayerException(t.toString());
        }
    }

    @Override
    public UserDTO findUserByName(String username) throws DAOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }   
}
